package top.lyc829.zzl_android.ui.profile.follow.user_detail;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Response;
import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Url;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class OtherUserDetailViewModel extends ViewModel {
    private MutableLiveData<UserInfoBean> userInfoBeanMutableLiveData;

    public OtherUserDetailViewModel() {
        this.userInfoBeanMutableLiveData = new MutableLiveData<>();
    }

    private MutableLiveData<Integer> isFellowLiveData;

    public MutableLiveData<UserInfoBean> getUserInfoBeanMutableLiveData() {
        if(userInfoBeanMutableLiveData == null) {
            userInfoBeanMutableLiveData = new MutableLiveData<>();
        }
        return userInfoBeanMutableLiveData;
    }

    public MutableLiveData<Integer> getIsFellowLiveData() {
        if (isFellowLiveData == null) {
            isFellowLiveData = new MutableLiveData<>();
        }
        return isFellowLiveData;
    }


    public void getUserInfo(int userId) {

        HashMap<String, String> body = new HashMap<>();
        body.put("user_id", String.valueOf(userId));

        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_OTHERUSER_INFO, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();

                Utils.Sout(resultStr);
                ResultBean<UserInfoBean> resultBean = JsonUtilNew.fromJsonObject(resultStr, UserInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getUserInfoBeanMutableLiveData().postValue(resultBean.getData());
                }

            }

            @Override
            public void failed(IOException e) {

                Utils.showToastSync("查询失败失败");
            }
        });

    }

    /**
     * 获取关注关系
     * @param userId 用户id
     * @param type 类型
     * @param typeId 目的id
     */
    public void getFellowRelationShip(int userId, int type, int typeId) {
        Network.getAttentionRelationship(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                TypeToken<ResultBean<Integer>> typeToken = new TypeToken<ResultBean<Integer>>() {};
                ResultBean<Integer> resultBean = new Gson().fromJson(resultStr, typeToken.getType());

                if (resultBean.getCode().equals(200)) {
                    getIsFellowLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                getIsFellowLiveData().postValue(0);
                Utils.Sout(e.getMessage());
            }
        });
    }
}
